package com.example.yichuangyun.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogTreeBinding;
import com.example.yichuangyun.ui.adapter.SetShareAdapter;
import com.example.yichuangyun.utils.ScreenUtil;
import com.example.yichuangyun.utils.ToastHelper;
import com.example.yichuangyun.utils.treelist.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业架构弹窗
 */
public class TreeDialog {
    private Dialog dialog;
    private DialogTreeBinding binding;
    private Node mNode = null;
    private final List<Node> mData;
    private SetShareAdapter shareAdapter;
    private final Context context;
    private boolean isSingle = true;

    private final List<Node> chooseData = new ArrayList<>();

    public TreeDialog(Context context, List<Node> mData) {
        this.mData = mData;
        this.context = context;
        if (dialog == null) {
            getDialog(context);
        }
    }

    public TreeDialog(Context context, List<Node> mData, boolean isSingle) {
        this.mData = mData;
        this.context = context;
        this.isSingle = isSingle;
        if (dialog == null) {
            getDialog(context);
        }
    }

    private void getDialog(Context context) {
        binding = DialogTreeBinding.inflate(LayoutInflater.from(context));
        dialog = new Dialog(context, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtil.getMaxHeight(context) * 3 / 4));
        dialog.setCancelable(true);
        Window dialogWindow = dialog.getWindow();
        // 去除遮罩效果
//        dialogWindow.setDimAmount(0f);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.main_menu_animStyle);
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);

        shareAdapter = new SetShareAdapter(context, mData, 1, isSingle);
        binding.recyclerView.setAdapter(shareAdapter);
        shareAdapter.setOnOnSingleCheckListener(node -> {
            if (node.isChecked()) {
                mNode = node;
                String text = String.format(context.getString(R.string.already_choose_person), node.getName());
                binding.tvNum.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
            } else {
                binding.tvNum.setText(context.getString(R.string.hint_choose_please));
            }
            confirm(mNode);
        });

        shareAdapter.setOnOnCheckListener(node -> {
            if (node.isChecked()) {
                chooseData.add(node);
            } else {
                chooseData.removeIf(node1 -> node1.getId().equals(node.getId()));
            }

            if (chooseData.size() > 0) {
                String text = context.getString(R.string.choose_share_person_count, chooseData.size());
                binding.tvNum.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
            } else {
                binding.tvNum.setText(context.getString(R.string.hint_choose_please));
            }
            confirm(chooseData);
        });

        binding.tvConfirm.setOnClickListener(v -> {
            if (binding.tvNum.getText().toString().equals(context.getString(R.string.hint_choose_please))) {
                ToastHelper.show(binding.tvTitle.getText().toString());
            } else {
                if (isSingle) {
                    confirm(mNode);
                } else {
                    confirm(chooseData);
                }
                dismissDialog();
            }
        });

        binding.ivDelete.setOnClickListener(v -> dismissDialog());

        binding.etSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String content = binding.etSearch.getText().toString();
                mData.clear();
                search(content, mNode);
                return true;
            }
            return false;
        });

        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                binding.ivClear.setVisibility(editable.toString().length() == 0 ? View.GONE : View.VISIBLE);
            }
        });

        binding.ivClear.setOnClickListener(view -> {
            binding.etSearch.setText("");
            mData.clear();
            search("", mNode);
        });
    }

    public void confirm(Node node) {

    }

    public void confirm(List<Node> nodes) {

    }

    public void search(String keyword, Node node) {

    }

    public void setTitle(String title) {
        binding.tvTitle.setText(title);
    }

    public void setFlag(int flag) {
        shareAdapter.setFlag(flag);
    }

    public void reset() {
        for (Node node : mData) {
            node.setChecked(false);
        }
        shareAdapter.notifyDataSetChanged();
        binding.tvNum.setText(context.getString(R.string.hint_choose_please));
        binding.etSearch.setText("");
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }

    public boolean isShow() {
        return dialog.isShowing();
    }

    public void refreshData(int defaultExpandLevel) {
        shareAdapter.addDataAll(mData, defaultExpandLevel);
    }
}
